package com.axc.gallery2.edit.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.axc.gallery2.edit.EditActivity;
import com.axc.gallery2.edit.ToReleaseBitmap;
import com.axc.gallery2.edit.effective.ToZoom;
import com.axc.gallery2.function.ISaveBitmap;

public class CircleRenderView extends View implements Runnable,
		OnTouchListener, ISetViewMenu, ISaveBitmap {

	int imageID = 0;
	int x, y = 0;
	int circleWidth = 0;

	public static CircleRenderView cview;
	public static Bitmap bitmap = null;
	public static Bitmap bitmap2 = null;
	int width = 0;
	int height = 0;
	BlurMaskFilter blurMaskFilter;
	Paint paint = null;
	Shader mBitmapShader = null;
	ShapeDrawable shapeDrawable = null;
	Canvas canvas1;

	int outterColor = 0;
	int innerColor = 0;

	/*
	 * 线性渐变渲染 Shader mLinearGradient = null;
	 */

	/* 混合渲染 */
	Shader mComposeShader = null;
	/* 唤醒渐变渲染 */

	public static Shader mRadialGradient = null;
	/* 梯度渲染 */

	Shader mSweepGradient = null;

	int w,h=0;
	public CircleRenderView(Context context, int w, int h) {
		super(context);
		cview = this;
		this.setOnTouchListener(this);
		circleWidth = 150;

		tt(w, h);
	}

	public void tt(int w, int h) {
		this.w=w;
		this.h=h;
		blurMaskFilter = new BlurMaskFilter(50, Blur.SOLID);

		/**
		 * 获取图片
		 */
		bitmap =new ToReleaseBitmap().RelBmp(EditActivity.imageFileNames);//BitmapFactory.decodeFile(EditActivity.imageFileNames);
		// bitmap = ((BitmapDrawable)
		// getResources().getDrawable(CircleRenderActivity.imageID)).getBitmap();

		width = w;// bitmap.getWidth();
		height = h;// bitmap.getHeight();
		// bitmap = new ToZoomImage().ProcessImage(bitmap, 440, 550);
		// Log.w("ww", String.valueOf(bitmap.getWidth())); 原图大小440x550
		// bitmap=new ToZoomPercentage().ProcessImage(bitmap, (float)0.8);
		// 高度大于宽度
		bitmap = new ToZoom().ProcessImage(bitmap, w, h);
		// 高度小于宽度
		// bitmap=new ToZoom().ProcessImage(bitmap, w,h);
		/* 构建RadialGradient对象，设置半径的属性 */

		// x,y 半径,颜色 （TitleMode）显示模式 {1CLAMP 复制边缘颜色如果材质吸引它原来的范围之外,2MIRROR
		// 镜像重复,3REPEAT 重复图像}

		bitmap2 = bitmap;

		// 渐变光圈效果一
		mRadialGradient = new RadialGradient(100, 100, circleWidth, new int[] {
				Color.WHITE, Color.YELLOW }, null, Shader.TileMode.CLAMP);

		// TODO Auto-generated constructor stub
		mBitmapShader = new BitmapShader(bitmap2, TileMode.REPEAT,
				TileMode.MIRROR);

		// 混合模式
		mComposeShader = new ComposeShader(mBitmapShader, mRadialGradient,
				PorterDuff.Mode.DARKEN);
		paint = new Paint();
		// 可编辑宽度 高度
		// bitmap2=bitmap; //= Bitmap.createBitmap(w-40, height-160,
		// Config.RGB_565);
		canvas1 = new Canvas(bitmap2);
		outterColor = Color.YELLOW;
		innerColor = Color.WHITE;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		Log.w("touch", "开始");
		shapeDrawable = new ShapeDrawable(new RectShape());
		shapeDrawable.getPaint().setShader(mBitmapShader);
		shapeDrawable.setBounds(0, 0, width, height);
		shapeDrawable.draw(canvas);
		// shapeDrawable.draw(canvas1);

		// 绘制渐变的矩形

		// paint.setShader(mLinearGradient);

		// canvas.drawRect(150, 160, 300, 300, paint);

		// 显示混合渲染效果

		paint.setShader(mComposeShader);
		paint.setMaskFilter(blurMaskFilter);

		canvas.drawRect(0, 0, width, height, paint);

		// 绘制环形渐变

		// paint.setShader(mRadialGradient);

		// canvas.drawCircle(50, 200, 50, paint);

		// 绘制梯度渐变

		// paint.setShader(mSweepGradient);

		// canvas.drawRect(150, 160, 300, 300, paint);
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (!Thread.currentThread().isInterrupted()) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}

			// 使用postInvalidate可以直接在线程中更新界面
			myRun(x, y);
			postInvalidate();

		}

	}

	public void myRun(int x, int y) {
		mRadialGradient = new RadialGradient(x, y, circleWidth, new int[] {
				innerColor, outterColor }, null, Shader.TileMode.CLAMP);
		// 混合模式
		mComposeShader = new ComposeShader(mBitmapShader, mRadialGradient,
				PorterDuff.Mode.DARKEN);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getAction()) {

		case MotionEvent.ACTION_MOVE:
			Log.w("touch", "touch move");
			x = (int) event.getX();
			y = (int) event.getY();
			new Thread(this).start();
			break;
		default:
			break;
		}
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
			Log.w("w", "left");
			circleWidth += 30;
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
			Log.w("w", "right");
			if (circleWidth > 30)
				circleWidth -= 30;
		}

		new Thread(this).start();
		return true;
	}

	public void InnerColor(int color) {
		innerColor = color;
	}

	public void OutterColor(int color) {
		outterColor = color;
	}

	@Override
	public void setOutColor(int color) {
		// TODO Auto-generated method stub
		OutterColor(color);
		new Thread(this).start();
	}

	@Override
	public void setInnerColor(int color) {
		// TODO Auto-generated method stub
		InnerColor(color);
		new Thread(this).start();
	}

	@Override
	public void setCircleRadius(int circleWidth) {
		// TODO Auto-generated method stub
		this.circleWidth = circleWidth;
		new Thread(this).start();
	}

	@Override
	public void ChangeInOutColor() {
		// TODO Auto-generated method stub
		int temp = innerColor;
		innerColor = outterColor;
		outterColor = temp;
		new Thread(this).start();
	}

	@Override
	public void Save() {
		// TODO Auto-generated method stub
		canvas1.drawRect(0, 0, width, height, paint);
		postInvalidate();
	} 

	@Override
	public void Reset() {
		// TODO Auto-generated method stub

		bitmap2 = bitmap;
		tt(w, h);
	}

}
